import { RailData, RailRes, LoadType, RailShapeType } from '@/api/waybill/index'

import { wgs84ToGCJ02, toGCJ02PointArr } from '@/utils/mapUtil'

import type { VehicleTrackData } from '@/api/vehicle-track/vehicleTrack'

const colorOption = {
  [LoadType.Load]: {
    fillColor: '#B1F1D0',
    strokeColor: '#01B257'
  },
  [LoadType.Unload]: {
    fillColor: '#EECABB',
    strokeColor: '#D6501C'
  }
}
// 初始化围栏
export const createRails = (AMap: any, railsData: RailData[]) => {
  const overlayArr = []
  railsData.forEach(rail => {
    let overlay = null
    const type = rail.loadType
    const option = {
      cursor: 'pointer',
      ...colorOption[type]
    }
    if (rail.shapeType === RailShapeType.Circle) {
      overlay = new AMap.Circle({
        center: wgs84ToGCJ02(rail.lng, rail.lat),
        radius: rail.radius, //半径
        ...option
      })
    } else if (rail.shapeType === RailShapeType.Rectangle) {
      const railData = toGCJ02PointArr(JSON.parse(rail.railData))
      const bounds = new AMap.Bounds(railData[0][0], railData[0][2])
      overlay = new AMap.Rectangle({
        bounds,
        ...option
      })
    } else if (rail.shapeType === RailShapeType.Polygon) {
      let path = toGCJ02PointArr(JSON.parse(rail.railData))
      if (rail.railType === 1) {
        for (let i = 0; i < path.length; i++) {
          path[i] = [path[i]]
        }
      } else {
        path = path[0]
      }

      overlay = new AMap.Polygon({
        path,
        ...option
      })
    }
    if (overlay) {
      overlay.setExtData(rail)
      overlayArr.push(overlay)
    }
  })
  return overlayArr
}

export function createMapPolyline(AMap: any, trackData: VehicleTrackData[]) {
  return new AMap.Polyline({
    path: trackData.map(data => wgs84ToGCJ02(data.lng, data.lat)),
    strokeColor: '#0066ff',
    showDir: true, // 显示方向
    strokeWeight: 6, // 线宽
    strokeOpacity: 1,
    lineCap: 'round'
  })
}

export function getAssetsFile(url: string) {
  return new URL(`../../assets/images/${url}`, import.meta.url).href
}

type MarkerParams = VehicleTrackData & {
  content: string
}

export function createMarker(AMap: any, params: MarkerParams): any
export function createMarker(AMap: any, position: VehicleTrackData, url: string): any
export function createMarker(AMap, params, url?: string) {
  const lngLat = wgs84ToGCJ02(params.lng, params.lat)
  if (isMarkerParams(params)) {
    // 创建
    const vehicleMarker = new AMap.Marker({
      position: lngLat,
      // angle: params.course ?? 0,
      content: params.content,
      offset: new AMap.Pixel(-16, -16)
    })
    return vehicleMarker
  }
  const icon = new AMap.Icon({
    size: new AMap.Size(32, 32),
    image: getAssetsFile(url),
    imageSize: new AMap.Size(32, 32),
    imageOffset: new AMap.Pixel(0, 0)
  })

  return new AMap.Marker({
    position: lngLat,
    icon: icon,
    offset: new AMap.Pixel(-13, -30)
  })
}

function isMarkerParams(param: MarkerParams | VehicleTrackData): param is MarkerParams {
  return Object.hasOwn(param, 'content')
}

// 添加围栏类型
export function initRailType(rails: RailRes[], loadType: LoadType) {
  return rails.map(rail => ({
    ...rail,
    loadType
  }))
}
